package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbUserMessage;
import com.beetlestech.nfe.main.biz.com.dto.MessageInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.UsermessageDTO;
import com.beetlestech.nfe.main.db.view.VmessagelistDTO;


public class HbUserMessageImpl extends AbstractDBLogic implements HbUserMessage, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "用户信息表";

	public MessageInfoDTO[] getUserMessageList(String messageID, Integer userID, Integer readflg){
		List<MessageInfoDTO> messageList = new ArrayList<MessageInfoDTO>();
		
		if(messageID == null)
		{
			messageList = getMessageList(userID, readflg);
		}
		else
		{
			messageList.add(getUserMessage(Integer.parseInt(messageID)));
		}
		return messageList.toArray(new MessageInfoDTO[messageList.size()]);
	}
	
	public void updateMessageReadFlg(String msgID){
		UsermessageDTO updateUserMessageDTO = new UsermessageDTO();
		for(String item : msgID.split(",")){
			if(!StringUtil.isEmpty(item))
			{
				updateUserMessageDTO.setMsgId(Integer.parseInt(item));
				updateUserMessageDTO.setReadflg(1);
				updateUserMessageDTO.setLastDate(getCurrentDate());
				updateUserMessageDTO.setLastTime(getCurrentTime());
				updateDAO.updateOne(UsermessageDTO.UPDATE, updateUserMessageDTO, M10190WS, SH_INFO);
			}
		}
		
	}
	
	public void deleteMessage(String msgID){
		UsermessageDTO updateUserMessageDTO = new UsermessageDTO();
		for(String item : msgID.split(",")){
			if(!StringUtil.isEmpty(item))
			{
				updateUserMessageDTO.setMsgId(Integer.parseInt(item));
				updateDAO.updateOne(UsermessageDTO.DELETE, updateUserMessageDTO, M10190WS, SH_INFO);			
			}
		}
	}
	
	private List<MessageInfoDTO> getMessageList(Integer userID, Integer readflg){
		List<MessageInfoDTO> messageList = new ArrayList<MessageInfoDTO>();
		
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VmessagelistDTO.COLUMN_RECEIVEID, StatementUtil.KEYWORD.EQUAL, userID);
		if(readflg != null){
			StatementUtil.andWhere(statementDTO, VmessagelistDTO.COLUMN_READFLG, StatementUtil.KEYWORD.EQUAL, readflg);
		}
		VmessagelistDTO vmessagelistDTO = new VmessagelistDTO();
		vmessagelistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		List<VmessagelistDTO> VMessages = queryDAO.queryForObjectList(VmessagelistDTO.SQL_SEARCH, vmessagelistDTO);
		for(VmessagelistDTO item : VMessages){
			messageList.add(convertToMessageInfo(item));
		}
		
		return messageList;
	}
	
	private MessageInfoDTO getUserMessage(Integer messageID){
		MessageInfoDTO messageInfo = null;
		
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VmessagelistDTO.COLUMN_MSGID, StatementUtil.KEYWORD.EQUAL, messageID);
	
		VmessagelistDTO searchMessage = new VmessagelistDTO();
		searchMessage.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		searchMessage = queryDAO.queryForObject(VmessagelistDTO.SQL_SEARCH, searchMessage);
		
		if(searchMessage != null)
		{
			messageInfo = convertToMessageInfo(searchMessage);
		}

		return messageInfo;
	}
	
	private MessageInfoDTO convertToMessageInfo(VmessagelistDTO item){
		MessageInfoDTO messageInfo = new MessageInfoDTO();
		messageInfo.setMsgId(item.getMsgId());
		messageInfo.setMessage(item.getMessage());
		messageInfo.setFatherMsgId(item.getFatherMsgId());
		messageInfo.setReadflg(item.getReadflg());
		messageInfo.setReceiveId(item.getReceiveId());
		messageInfo.setReceiveName(item.getReceiveNM());
		messageInfo.setSendDT(item.getSendDT());
		messageInfo.setSenderId(item.getSenderId());
		messageInfo.setSenderName(item.getSenderNM());
		messageInfo.setSendTime(item.getSendTime());
		messageInfo.setTitel(item.getTitel());
		return messageInfo;
	}
}
